package ru.ifmo.cppsc.grammar;

import java.util.ArrayList;


/**
 * Author: Olga Komaleva
 * Date: Jun 17, 2008
 */
public class Production
{
    private final Symbol left;
    private final Symbol[] right;
    private final String code;

     public Production(Symbol left, Symbol[] right, String code) {
        this.left = left;
        this.right = right;
        this.code = code;
    }

    public Symbol getLeft() {
        return left;
    }

    public Symbol[] getRight() {
        return right;
    }

    public String getCode() {
        return code;
    }

    public void print() {
        System.out.print(left.getName() + " -> ");

        for (Symbol r : getRight()) {
            System.out.print(r.getName() + " ");
        }
        System.out.println("");
    }

    public Production removeFromRight(int i) {
        assert(i >= 0) : i;
        assert(i < right.length);

        Production p = new Production(left, new Symbol[right.length - 1], code );

        System.arraycopy(right, 0, p.right, 0, i);
        System.arraycopy(right, i + 1, p.right, i, right.length - i - 1);

        return p;
    }
    
    public Production removeFromRight(int i, Production p1) {
        return new CompoundProduction(this, i, p1);
    }

    public boolean isCompoundProduction() {
        return false;
    }

    public String toString () {
        String str = getLeft().getName() + " ";
        for ( Symbol sym : get().getRight()) {
            str += sym.getName() + " ";
        }
        str = str.substring( 0, str.length() - 1);
        return str;
    }

    public Production get() {
        return this;
    }
}


